<template>
    <div>
        <el-form style="width: 60%" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="图书ID">
                <el-input v-model="ruleForm.bookId" readonly></el-input>
            </el-form-item>
            <el-form-item label="图书名称" prop="name">
                <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="作者" prop="author">
                <el-input v-model="ruleForm.author"></el-input>
            </el-form-item>
            <el-form-item label="空闲书" prop="leisure">
                <el-input v-model="ruleForm.leisure"></el-input>
            </el-form-item>
            <el-form-item label="总数量" prop="sum">
                <el-input v-model="ruleForm.sum"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">修改</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                ruleForm: {
                    bookId: '',
                    name: '',
                    author: '',
                    leisure: '',
                    sum: ''
                },
                rules: {
                    name: [
                        {required: true, message: '图书名不能为空', trigger: 'blur'},
                        {min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur'}
                    ],
                    author: [
                        {required: true, message: '作者不能为空', trigger: 'blur'}
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                const _this = this;
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.put('http://localhost:8081/book/update',this.ruleForm).then(function (resp) {
                            if (resp) {
                                // _this.$message(resp.data.message);
                                _this.$alert('《' + _this.ruleForm.name + '》修改成功', '消息', {
                                    confirmButtonText: '确定',
                                    callback: action => {
                                        _this.$router.push("/BookManage");
                                    }
                                })
                            } else {
                                return false;
                            }
                        });
                    }
                })
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        },
        created() {
            const _this = this;
            axios.get('http://localhost:8081/book/findById/'+this.$route.query.bookId).then(function (resp) {
                _this.ruleForm = resp.data;
            })

        }
    }
</script>